package usf.gwt.ui.bootstrap.client;

import usf.gwt.ui.bootstrap.client.core.JqueryUtils;

import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.SpanElement;
import com.google.gwt.user.client.DOM;

public class FormGroup extends Layout {
	
	private static final String FORM_GROUP_STYLE = "form-group";
	
	private SpanElement span;

	@Override
	protected Element initWidget() {
		span = DOM.createSpan().cast();
		return super.initWidget(); 
	}
	
	@Override
	protected void initStyle() {
		span.setClassName("help-block");
		getElement().setClassName(FORM_GROUP_STYLE);
	}
	
	public void setValidationState(ValidationStates state) {
		JqueryUtils.switchClass(getElement(), ValidationStates.values(), state);
		resetHelpBlock();
	}
	public void setValidationState(ValidationStates state, String msg) {
		setValidationState(state);
		if(msg == null) resetHelpBlock();
		else setHelpBlock(msg);
	}
	
	protected void resetHelpBlock(){
		span.setInnerText("");
		span.removeFromParent();
	}
	protected void setHelpBlock(String text){
		span.setInnerHTML(text);
		getElement().appendChild(span);
	}
	
}
